package com.zb.zeus.system.api.factory;

import com.zb.zeus.common.core.constant.HttpStatus;
import com.zb.zeus.common.core.domain.R;
import com.zb.zeus.common.core.web.domain.AjaxResult;
import com.zb.zeus.common.core.web.page.TableDataInfo;
import com.zb.zeus.system.api.RemoteUserService;
import com.zb.zeus.system.api.domain.SysEmployee;
import com.zb.zeus.system.api.model.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
 * 用户服务降级处理
 * 
 * @author zb.zeus
 */
@Component
public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserService>
{
    private static final Logger log = LoggerFactory.getLogger(RemoteUserFallbackFactory.class);

    @Override
    public RemoteUserService create(Throwable throwable)
    {
        log.error("用户服务调用失败:", throwable);
        return new RemoteUserService()
        {
            @Override
            public R<LoginUser> getUserInfo(String username)
            {
                return R.fail("获取用户失败:" + throwable.getMessage());
            }

            @Override
            public R<LoginUser> getOuterUserInfo(String phn) {
                return R.fail("获取token用户信息失败:" + throwable.getMessage());
            }

            @Override
            public R<LoginUser> getTokenUserInfo(String token) {
                return R.fail("获取token用户信息失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult createToken(LoginUser loginUser) {
                return AjaxResult.error("创建用户Token失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult refreshToken(LoginUser loginUser) {
                return AjaxResult.error("刷新用户Token失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult delToken(String token) {
                return AjaxResult.error("删除用户Token失败:" + throwable.getMessage());
            }

            @Override
            public TableDataInfo feignList(SysEmployee sysEmployee, int pageNum, int pageSize) {
                TableDataInfo rspData = new TableDataInfo();
                rspData.setCode(HttpStatus.TIMEOUT);
                rspData.setRows(Collections.emptyList());
                rspData.setMsg("系统开小差, 请请稍后重试");
                rspData.setTotal(0L);
                return rspData;
            }
        };
    }
}
